import {} from 'cloud'
import {
  ui
} from './ui'

const db = wx.cloud.database()

export const ds = {
  coll: function (dsName) {
    return db.collection(dsName)
  },
  cmd: db.command,
  checkTrue(value) {
    if (value == undefined) return undefined
    return value != undefined && value != "false" && value != false
  },
  update: function (dsName, id, data, cb) {
    const that = this
    data = this.clone(data)
    data._id = undefined
    data._openid = undefined
    if (id) {
      that.coll(dsName).doc(id).update({
        data: data
      }).then(res => {
        // ui.toast(true, '更新成功')
        if (cb) cb(res)
      }).catch(err => {
        // ui.toast(false, '更新失败')
        console.error(err)
      })
    } else {
      that.coll(dsName).add({
        data: data
      }).then(res => {
        // ui.toast(true, '添加成功')
        if (cb) cb(res)
      }).catch(err => {
        // ui.toast(false, '添加失败')
        console.error(err)
      })
    }
  },
  remove: function (dsName, where, cb) {
    this.coll(dsName).where(where).remove()
      .then(res => {
        // ui.toast(true, '成功删除：' + res.stats.removed + '行数据')
        if (cb) cb(res)
      })
      .catch(err => {
        // ui.toast(false, '删除失败')
        console.error(err)
      })
  },
  cloudUpdate(dsName, id, data, cb) {
    data = this.clone(data)
    data._id = undefined
    wx.cloud.callFunction({
      name: 'dsUpdate',
      data: {
        _id: id,
        dsName: dsName,
        data: data
      }
    }).then(res => {
      // ui.toast(true, data._id ? '更新成功' : '添加成功')
      if (cb) cb(res)
    }).catch(err => {
      // ui.toast(false, data._id ? '更新失败' : '添加失败')
      console.error(err)
    })
  },
  cloudUpdateWhere(dsName, where, data, cb) {
    // console.log(dsName, where, data)
    data = this.clone(data)
    data._id = undefined
    data._openid = undefined
    wx.cloud.callFunction({
      name: 'dsUpdateWhere',
      data: {
        dsName: dsName,
        where: where,
        data: data
      }
    }).then(res => {
      // ui.toast(true, '操作成功')
      if (cb) cb(res)
    }).catch(err => {
      // ui.toast(false, '操作失败')
      console.error(err)
    })
  },
  cloudRemove: function (dsName, where, cb) {
    wx.cloud.callFunction({
      name: 'dsRemove',
      data: {
        dsName: dsName,
        where: where
      }
    }).then(res => {
      // ui.toast(true, '删除成功')
      if (cb) cb(res)
    }).catch(err => {
      // ui.toast(false, '删除失败')
      console.error(err)
    })
  },
  cloudGetWhere(dsName, where, cb) {
    wx.cloud.callFunction({
      name: 'dsGet',
      data: {
        dsName: dsName,
        where
      }
    }).then(res => {
      if (cb) cb(res)
    }).catch(err => {
      console.error(err)
    })
  },
  cloudGetByOpenid(dsName, cb) {
    this.cloudGetWhere(dsName, {
      _openid: true
    }, cb)
  },
  clone(obj) {
    const objStr = JSON.stringify(obj)
    return JSON.parse(objStr)
  },
  async getAll(dsName, where, field) {
    const totalRes = await db.collection(dsName)
      .where(where)
      .count()
    const total = totalRes.total,
      limitNum = 20
    const times = Math.ceil(total / limitNum)
    let resArr = []
    for (let i = 0; i < times; ++i) {
      const res = await db.collection(dsName)
        .where(where)
        .field(field ?? {})
        .skip(i * limitNum)
        .limit(limitNum)
        .get()
      resArr = resArr.concat(res.data)
    }
    return resArr
  }
}